/*
 * Created by JFormDesigner on Sat Jun 17 14:42:04 CST 2023
 */

package fm.customer;

import dao.Dao_customer;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

/**
 * @author YangC
 */
public class AddCustomer extends JInternalFrame {
    public AddCustomer() {
        initComponents();
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void add(ActionEvent e) {

        List<String> list = new ArrayList<>();
        list = Dao_customer.getCusID();

        if(cusIDTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入客户编号！");
            return;
        }

        for (int i = 0; i < list.size(); i++)
        {
            if(list.get(i).equals(cusIDTextField.getText()))
            {
                JOptionPane.showMessageDialog(null,"用户ID已存在！请重新设置用户编号");
                return;
            }
        }

        if(cusIDTextField.getText().length() != 4)
        {
            JOptionPane.showMessageDialog(null,"客户编号应为4位！");
            return;
        }

        if(cusNameTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入客户姓名！");
            return;
        }

        if(cusPhoneTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入电话！");
            return;
        }

        if(cusPhoneTextField.getText().trim().length() > 11)
        {
            JOptionPane.showMessageDialog(null,"联系电话输入有误！");
            return;
        }

        if(cusAddressTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入地址！");
            return;
        }

        String cusID = cusIDTextField.getText().trim();
        String cusName = cusNameTextField.getText().trim();
        String cusPhone = cusPhoneTextField.getText().trim();
        String cusAddress = cusAddressTextField.getText().trim();

        int t = Dao_customer.addCus(cusID, cusName, cusPhone, cusAddress);
        if(t == 1)
        {
            JOptionPane.showMessageDialog(null,"添加成功！");
            doDefaultCloseAction();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        addButton = new JButton();
        cancelButton = new JButton();
        panel3 = new JPanel();
        label1 = new JLabel();
        cusIDTextField = new JTextField();
        label2 = new JLabel();
        cusNameTextField = new JTextField();
        cusPhoneTextField = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        cusAddressTextField = new JTextField();
        label5 = new JLabel();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u6dfb\u52a0\u5ba2\u6237");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- addButton ----
            addButton.setText("\u6dfb\u52a0");
            addButton.addActionListener(e -> add(e));
            panel1.add(addButton);
            addButton.setBounds(new Rectangle(new Point(133, 3), addButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(283, 3), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 326, 473, panel1.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u5ba2\u6237\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(55, 30, label1.getPreferredSize().width, 18);
            panel3.add(cusIDTextField);
            cusIDTextField.setBounds(110, 25, 110, cusIDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u59d3\u540d\uff1a");
            panel3.add(label2);
            label2.setBounds(260, 30, label2.getPreferredSize().width, 19);
            panel3.add(cusNameTextField);
            cusNameTextField.setBounds(295, 25, 119, cusNameTextField.getPreferredSize().height);
            panel3.add(cusPhoneTextField);
            cusPhoneTextField.setBounds(111, 75, 110, cusPhoneTextField.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            panel3.add(label3);
            label3.setBounds(56, 80, label3.getPreferredSize().width, 20);

            //---- label4 ----
            label4.setText("\u5730\u5740\uff1a");
            panel3.add(label4);
            label4.setBounds(261, 80, label4.getPreferredSize().width, 22);
            panel3.add(cusAddressTextField);
            cusAddressTextField.setBounds(296, 75, 120, cusAddressTextField.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 180, 473, 146);

        //---- label5 ----
        label5.setIcon(new ImageIcon(getClass().getResource("/ref/img.png")));
        contentPane.add(label5);
        label5.setBounds(-10, -5, 495, 205);

        contentPane.setPreferredSize(new Dimension(485, 395));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton addButton;
    private JButton cancelButton;
    private JPanel panel3;
    private JLabel label1;
    private JTextField cusIDTextField;
    private JLabel label2;
    private JTextField cusNameTextField;
    private JTextField cusPhoneTextField;
    private JLabel label3;
    private JLabel label4;
    private JTextField cusAddressTextField;
    private JLabel label5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
